package m202405.d14;

import java.util.*;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/14 08:56
 */
public class Q46 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new LinkedList<>();
        doPermute(nums, 0, res, new LinkedHashSet<>());
        return res;
    }

    private void doPermute(int[] nums, int index, List<List<Integer>> res, Set<Integer> set) {
        if (index == nums.length) {
            res.add(new LinkedList<>(set));
        }
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            doPermute(nums, index + 1, res, set);
            set.remove(nums[i]);
        }
    }
}
